{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find All Lonely Numbers in the Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findLonely"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出数组中的所有孤独数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> 。如果数字 <code>x</code> 在数组中仅出现 <strong>一次</strong> ，且没有 <strong>相邻</strong> 数字（即，<code>x + 1</code> 和 <code>x - 1</code>）出现在数组中，则认为数字 <code>x</code> 是 <strong>孤独数字</strong> 。</p>\n",
    "\n",
    "<p>返回<em> </em><code>nums</code> 中的 <strong>所有</strong> 孤独数字。你可以按 <strong>任何顺序</strong> 返回答案。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [10,6,5,8]\n",
    "<strong>输出：</strong>[10,8]\n",
    "<strong>解释：</strong>\n",
    "- 10 是一个孤独数字，因为它只出现一次，并且 9 和 11 没有在 nums 中出现。\n",
    "- 8 是一个孤独数字，因为它只出现一次，并且 7 和 9 没有在 nums 中出现。\n",
    "- 5 不是一个孤独数字，因为 6 出现在 nums 中，反之亦然。\n",
    "因此，nums 中的孤独数字是 [10, 8] 。\n",
    "注意，也可以返回 [8, 10] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,3,5,3]\n",
    "<strong>输出：</strong>[1,5]\n",
    "<strong>解释：</strong>\n",
    "- 1 是一个孤独数字，因为它只出现一次，并且 0 和 2 没有在 nums 中出现。\n",
    "- 5 是一个孤独数字，因为它只出现一次，并且 4 和 6 没有在 nums 中出现。\n",
    "- 3 不是一个孤独数字，因为它出现两次。\n",
    "因此，nums 中的孤独数字是 [1, 5] 。\n",
    "注意，也可以返回 [5, 1] 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-all-lonely-numbers-in-the-array](https://leetcode.cn/problems/find-all-lonely-numbers-in-the-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-all-lonely-numbers-in-the-array](https://leetcode.cn/problems/find-all-lonely-numbers-in-the-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[10,6,5,8]', '[1,3,5,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.extend((inf, -inf))\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        for i in range(1, n-1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                ans.append(nums[i])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums += [-float(\"INF\"), float(\"INF\")]   # 避免额外判断边界条件\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(1, n - 1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                res.append(nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums += [-float(\"INF\"), float(\"INF\")]   # 避免额外判断边界条件\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(1, n - 1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                res.append(nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums += [-1e100, 1e100]\n",
    "        nums.sort()\n",
    "        lonely_list = []\n",
    "        for i in range(1, len(nums)-1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] -nums[i]>1:\n",
    "                lonely_list.append(nums[i])\n",
    "        return lonely_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "    \n",
    "        nums += [-2,1000002]\n",
    "        nums.sort()\n",
    "\n",
    "        return [ nums[i] for i in range(1,len(nums)-1) if nums[i+1] - nums[i] > 1 and nums[i] - nums[i-1] > 1]\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        l = len(nums)\n",
    "        i = 0\n",
    "        while i < l:\n",
    "            while i + 1 < l and nums[i + 1] == nums[i]:\n",
    "                i += 1\n",
    "            if i + 1 < l and nums[i + 1] == nums[i] + 1:\n",
    "                i += 1\n",
    "            elif i - 1 >= 0 and (nums[i - 1] == nums[i] or nums[i - 1] + 1 == nums[i]):\n",
    "                i += 1\n",
    "            else:\n",
    "                ans.append(nums[i])\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        if len(nums) == 1:\n",
    "            return [nums[0]]\n",
    "        result = []\n",
    "        if abs(nums[1] - nums[0]) <= 1:\n",
    "            result = []\n",
    "        else:\n",
    "            result.append(nums[0])\n",
    "\n",
    "        for i in range(1 , n - 1):\n",
    "            if abs(nums[i] - nums[i-1]) <= 1 or abs(nums[i] - nums[i+1]) <= 1:\n",
    "                continue\n",
    "            else:\n",
    "                result.append(nums[i])\n",
    "        \n",
    "        if abs(nums[n-1] - nums[n - 2]) <= 1:\n",
    "            return result\n",
    "        else:\n",
    "            result.append(nums[n-1])\n",
    "            return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums = [-2, inf] + nums\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        for i in range(1, len(nums) - 1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                ans.append(nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        sL = sorted(nums)\n",
    "        if len(sL) == 1:\n",
    "            return sL\n",
    "        if sL[1] - sL[0] > 1:\n",
    "            result.append(sL[0])\n",
    "        for i in range(1, len(sL) - 1):\n",
    "            if sL[i] - sL[i - 1] > 1 and sL[i + 1] - sL[i] > 1:\n",
    "                result.append(sL[i])\n",
    "        if sL[len(sL) - 1] - sL[len(sL) - 2] > 1:\n",
    "            result.append(sL[len(sL) - 1])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        if len(nums)==1:\n",
    "            return nums\n",
    "        nums = sorted(nums)\n",
    "        for i in range(1,len(nums)):\n",
    "            if i == len(nums)-1:\n",
    "                if nums[i] - nums[i-1]>=2:\n",
    "                    res.append(nums[i])\n",
    "            else:\n",
    "                if nums[i]-nums[i-1]>=2 and nums[i+1]-nums[i]>=2:\n",
    "                    res.append(nums[i])\n",
    "        if nums[1]-nums[0]>=2:\n",
    "            res.append(nums[0])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        l = len(nums)\n",
    "        i = 0\n",
    "        while i < l:\n",
    "            while i + 1 < l and (nums[i + 1] == nums[i] or nums[i + 1] == nums[i] + 1):\n",
    "                i += 1\n",
    "            if i - 1 >= 0 and (nums[i - 1] == nums[i] or nums[i - 1] + 1 == nums[i]):\n",
    "                i += 1\n",
    "            else:\n",
    "                ans.append(nums[i])\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return nums\n",
    "\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        \n",
    "        if nums[1] - nums[0] > 1:\n",
    "            ans.append(nums[0])\n",
    "        if nums[-1] - nums[-2] > 1:\n",
    "            ans.append(nums[-1])\n",
    "        for i in range(1, n-1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                ans.append(nums[i])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        return [nums[i] for i in range(len(nums)) if (0 < i < len(nums) - 1 and nums[i] - nums[i - 1] > 1 and nums[i + 1] - nums[i] > 1) or (i == 0 and nums[i + 1] - nums[i] > 1) or ((i == len(nums) - 1 and nums[i] - nums[i - 1] > 1))] if len(nums) >= 2 else nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        new_sums = []\n",
    "        nums.sort()\n",
    "        if len(nums) == 1:\n",
    "            return nums\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0:\n",
    "                if nums[i+1]-nums[i]>1:\n",
    "                    new_sums.append(nums[i])\n",
    "            elif i == len(nums)-1:\n",
    "                if nums[i]-nums[i-1]>1:\n",
    "                    new_sums.append(nums[i])\n",
    "            else:\n",
    "                if (nums[i]-nums[i-1]>1)&(nums[i+1]-nums[i]>1):\n",
    "                    new_sums.append(nums[i])\n",
    "        return new_sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        print(nums)\n",
    "        n = len(nums)\n",
    "        result = []\n",
    "        i = 0\n",
    "        flag = 0\n",
    "        while i+1<n:\n",
    "            if nums[i] == nums[i+1] or nums[i]+1 == nums[i+1]:\n",
    "                flag = 1\n",
    "            elif flag == 1:\n",
    "                flag = 0\n",
    "            elif flag == 0:\n",
    "                flag = 0\n",
    "                result.append(nums[i])\n",
    "            i += 1\n",
    "        if flag == 0:\n",
    "            result.append(nums[-1])\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(1, n - 1):\n",
    "            if nums[i] - nums[i-1] > 1 and nums[i+1] - nums[i] > 1:\n",
    "                res.append(nums[i])\n",
    "        if nums[n-1] - nums[n-2] > 1:\n",
    "            res.append(nums[n-1])\n",
    "        if n>1 and nums[1] - nums[0] > 1:\n",
    "            res.append(nums[0])\n",
    "        if n == 1:\n",
    "            res.append(nums[0])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        ptr = 0\n",
    "        ans = []\n",
    "        while ptr < n:\n",
    "            if (ptr - 1 == -1 or nums[ptr] - nums[ptr - 1] > 1) and (ptr + 1 == n or nums[ptr + 1] - nums[ptr] > 1):\n",
    "                ans.append(nums[ptr])\n",
    "            ptr += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums = [-2, inf] + nums\n",
    "        nums.sort()\n",
    "        return [nums[i] for i in range(1, len(nums) - 1) if nums[i]- nums[i-1] > 1 and nums[i+1]- nums[i] > 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "    \n",
    "        nums.sort()\n",
    "        nums.insert(0,nums[0]-2)\n",
    "        nums.append(nums[-1]+2)\n",
    "\n",
    "        return [ nums[i] for i in range(1,len(nums)-1) if nums[i+1] - nums[i] > 1 and nums[i] - nums[i-1] > 1]\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = []\n",
    "        for x, y in cnt.items():\n",
    "            if y == 1 and x+1 not in cnt and x-1 not in cnt:\n",
    "                ans.append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        for k, v in cnt.items():\n",
    "            if v == 1 and cnt[k+1] == 0 and cnt[k-1] == 0:\n",
    "                ans.append(k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        dic = Counter(nums)\n",
    "        res = []\n",
    "        for k,v in dic.items():\n",
    "            if v == 1 and k-1 not in dic and k+1 not in dic:\n",
    "                res.append(k)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        d = collections.Counter(nums)\n",
    "        ans = []\n",
    "        for num, cnt in d.items():\n",
    "            if cnt > 1: continue\n",
    "            if (num - 1 not in d) and (num + 1 not in d):\n",
    "                ans.append(num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        ret = []\n",
    "        cnt = Counter(nums)\n",
    "        for k, v in cnt.items():\n",
    "            if v == 1 and cnt[k-1] == 0 and cnt[k+1] == 0:\n",
    "                ret.append(k)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\r\n",
    "        s = Counter(nums)\r\n",
    "        return [x for x in nums if x - 1 not in s and x + 1 not in s and s[x] == 1]\r\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        cnt = Counter(nums)\n",
    "        \n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        if n == 1: return nums\n",
    "        for i, num in enumerate(nums):\n",
    "            if i == 0 and cnt[num] == 1 and nums[i+1] != num + 1: ans.append(num)\n",
    "            elif i == n - 1 and cnt[num] == 1 and nums[i-1] != num - 1: ans.append(num)\n",
    "            elif 0 < i < n - 1 and cnt[num] == 1 and nums[i-1] != num - 1 and nums[i+1] != num + 1: ans.append(num)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        c = Counter(nums)\n",
    "        res = []\n",
    "        for i in nums:\n",
    "            if c[i] == 1 and c[i - 1] == 0 and c[i + 1] == 0:\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = []\n",
    "        for x in cnt:\n",
    "            if cnt[x] == 1 and cnt[x-1] == 0 and cnt[x+1] == 0:\n",
    "                ans.append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        mp = collections.Counter(nums)\n",
    "        ans = []\n",
    "        for key, val in mp.items():\n",
    "            if val == 1 and key + 1 not in mp and key - 1 not in mp:\n",
    "                ans.append(key)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        dic = collections.Counter(nums)\n",
    "        ret = []\n",
    "        for k,v in dic.items():\n",
    "            if v == 1 and dic.get(k+1,0) == 0 and dic.get(k-1,0) == 0:\n",
    "                ret.append(k)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        d = collections.Counter(nums)\n",
    "        ans = []\n",
    "        for num, cnt in d.items():\n",
    "            if cnt > 1: continue\n",
    "            if (num - 1 not in d) and (num + 1 not in d):\n",
    "                ans.append(num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        return [x for x in nums if cnt[x] == 1 and x - 1 not in cnt and x + 1 not in cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt=Counter(nums)\n",
    "        ans=[]\n",
    "        for x in cnt:\n",
    "            if cnt[x]==1 and x-1 not in cnt and x+1 not in cnt:\n",
    "                ans.append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = []\n",
    "        for k, v in cnt.items():\n",
    "            if v == 1 and cnt[k - 1] + cnt[k + 1] == 0:\n",
    "                ans.append(k)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        c = Counter(nums)\n",
    "        return [x for x, v in c.items() if v == 1 and x - 1 not in c and x + 1 not in c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        for num in nums:\n",
    "            if cnt[num] == 1 and not cnt[num - 1] and not cnt[num + 1]:\n",
    "                ans.append(num)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        return [k for k, v in cnt.items() if v == 1 and k - 1 not in cnt and k + 1 not in cnt]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        count = Counter(nums)\n",
    "        for num in nums:\n",
    "            if count[num] == 1 and count[num-1] == 0 and count[num+1] == 0:\n",
    "                ans.append(num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        dic = defaultdict(int)\n",
    "        for v in nums:\n",
    "            dic[v]+=1\n",
    "        res = []\n",
    "        for k,v in dic.items():\n",
    "            if v == 1 and k-1 not in dic and k+1 not in dic:\n",
    "                res.append(k)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        # 统计每个数字出现的次数\n",
    "        counts = Counter(nums)\n",
    "        \n",
    "        arr = []\n",
    "        for num in nums:\n",
    "            # 数字出现一次且没有相邻数字，则为孤独数字\n",
    "            if counts[num] == 1 and counts[num+1]==0 and counts[num-1]==0:\n",
    "                arr.append(num)\n",
    "        \n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        freq = Counter(nums)   # 每个元素出现次数哈希表\n",
    "        for num in nums:\n",
    "            if freq[num-1] == 0 and freq[num+1] == 0 and freq[num] == 1:\n",
    "                res.append(num)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        freq = Counter(nums)\n",
    "        for num in nums :\n",
    "            if freq[num-1] == 0 and freq[num+1] == 0 and freq[num] == 1 :\n",
    "                res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = []\n",
    "        for x in nums:\n",
    "            if cnt[x] == 1 and x - 1 not in cnt and x + 1 not in cnt:\n",
    "                ans.append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        zd = dict()\n",
    "        fhsz = []\n",
    "        for i in nums:\n",
    "            zd[i] = zd.get(i, 0) + 1\n",
    "        for i in zd:\n",
    "            if zd[i] == 1 and i-1 not in zd and i+1 not in zd:\n",
    "                fhsz.append(i)\n",
    "        return fhsz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for i in nums:\n",
    "            if i in dic:\n",
    "                dic[i]+=1\n",
    "            else:\n",
    "                dic[i]=1\n",
    "        for k,v in dic.items():\n",
    "            if v==1 and k-1 not in dic and k+1 not in dic:\n",
    "                res.append(k)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        freq = Counter(nums)\n",
    "        for num in nums:\n",
    "            if freq[num - 1] == 0 and freq[num + 1] == 0 and freq[num] == 1:\n",
    "                res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        freq = Counter(nums)   # 每个元素出现次数哈希表\n",
    "        for num in nums:\n",
    "            if freq[num-1] == 0 and freq[num+1] == 0 and freq[num] == 1:\n",
    "                res.append(num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        fre = Counter(nums)\n",
    "        ans = []\n",
    "        for key in fre.keys():\n",
    "            if fre[key] == 1 and key + 1 not in fre.keys() and key - 1 not in fre.keys():\n",
    "                ans.append(key)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        a = Counter(nums)\n",
    "        return [x for x in a.keys() if a[x] == 1 and x+1 not in a.keys() and x-1 not in a.keys()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        dic = defaultdict(int)\n",
    "        for v in nums:\n",
    "            dic[v]+=1\n",
    "        res = []\n",
    "        for k,v in dic.items():\n",
    "            if v == 1 and k-1 not in dic and k+1 not in dic:\n",
    "                res.append(k)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        zd = dict()\n",
    "        fhsz = []\n",
    "        for i in nums:\n",
    "            zd[i] = zd.get(i, 0) + 1\n",
    "        for i in zd:\n",
    "            if zd[i] == 1 and i-1 not in zd and i+1 not in zd:\n",
    "                fhsz.append(i)\n",
    "        return fhsz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        for _, i in enumerate(nums):\n",
    "            if cnt[i] == 1 and cnt[i - 1] == 0 and cnt[i + 1] == 0:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        nums += [float('-inf') , float('inf')]\n",
    "        nums.sort()\n",
    "        hashmap = Counter(nums)\n",
    "        result = []\n",
    "        for i in range(1 , len(nums) - 1):\n",
    "            if abs(nums[i] - nums[i - 1]) > 1 and abs(nums[i] - nums[i + 1]) > 1:\n",
    "                result.append(nums[i])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        ans = set()\n",
    "        tmp = set()\n",
    "        for num in nums:\n",
    "            if num - 1 in ans or num in ans or num + 1 in ans:\n",
    "                tmp.add(num)\n",
    "                tmp.add(num - 1)\n",
    "                tmp.add(num + 1)\n",
    "            else:\n",
    "                ans.add(num)\n",
    "        return list(ans.difference(tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        d = Counter(nums)\n",
    "        ret = set()\n",
    "        for x in nums:\n",
    "            if d[x] == 1 and d[x - 1] == 0 and d[x + 1] == 0:\n",
    "                ret.add(x)\n",
    "        return list(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        C1=Counter(nums)\n",
    "        ss=set(nums)\n",
    "        dp=[]\n",
    "        for k in ss:\n",
    "            if not k-1 in ss and not k+1 in ss and C1[k]==1:\n",
    "                dp.append(k)\n",
    "        \n",
    "        return dp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        hashmap = set(nums)\n",
    "        for k, v in cnt.items():\n",
    "            if v == 1 and k + 1 not in hashmap and k - 1 not in hashmap:\n",
    "                ans.append(k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        a = Counter(nums)\n",
    "        t = set(x for x in a.keys() if a[x] == 1)\n",
    "        ans = []\n",
    "        for i in t:\n",
    "            if i+1 not in a.keys()  and i-1 not in a.keys() :\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        res=[]\n",
    "        arr=[0]*(10**6+1)\n",
    "        for i in nums:\n",
    "            arr[i]+=1\n",
    "        for i in range(len(arr)):\n",
    "            if arr[i]==1 and (i-1<0 or arr[i-1]==0) and (i+1>10**6 or arr[i+1]==0):\n",
    "                res.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonely(self, nums: List[int]) -> List[int]:\n",
    "        s = set(nums)\n",
    "        m = Counter(nums)\n",
    "        \n",
    "        ans = []\n",
    "        for x in nums:\n",
    "            if m[x] == 1 and x-1 not in s and x+1 not in s:\n",
    "                ans.append(x)\n",
    "        \n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
